package com.changgou.brand.service;

import changgou.brand.pojo.Brand;
import com.changgou.brand.mapper.BrandMapper;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.util.IdWorker;
import com.changgou.util.QiniuUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    /**
     * 品牌分页条件查询
     *
     * @param currentPage
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Map<String, Object> findBrandWithPage(Integer currentPage, Integer pageSize, String name) {

        //判断是否有分页参数，如果没有设置默认值
        if (currentPage == null && currentPage < 1) {
            currentPage = 1;
        }
        if (pageSize == null && pageSize < 1) {
            pageSize = 8;
        }

        //设置查询条件
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();

        //判断是否有查询条件
        if (!StringUtils.isEmpty(name)) {
            criteria.andLike("name", "%" + name + "%");
        }


        //使用分页插件开启分页
        Page<Brand> page = PageHelper.startPage(currentPage, pageSize);
        //查询所有品牌
        brandMapper.selectByExample(example);

        //封装返回参数
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", page.getTotal());
        map.put("brandList", page.getResult());

        return map;
    }

    @Autowired
    private IdWorker idWorker;

    /**
     * 品牌新增
     *
     * @param brand
     * @return
     */
    @Override
    public Boolean add(Brand brand) {
        //判断品牌名称和品牌log是否为空
        if (StringUtils.isEmpty(brand.getName())) {
            return false;
        }
        if (brand.getImage().isEmpty()) {
            return false;
        }

        //数据库主键是int类型，生成的长度太长，所以截取了生成的id长度
        String id = idWorker.nextId() + "";
        id = id.substring(0, 6);
        brand.setId(Integer.valueOf(id));
        //新增品牌
        brandMapper.insertSelective(brand);
        return true;
    }

    /**
     * 根据id查询品牌
     *
     * @param id
     * @return
     */
    @Override
    public Brand findById(String id) {
        // 判断id是否存在
        if (StringUtils.isEmpty(id)) {
            new RuntimeException("品牌id不存在");
        }
        return brandMapper.selectByPrimaryKey(id);
    }

    /**
     * 更新品牌
     *
     * @param brand
     * @return
     */
    @Override
    public Boolean update(Brand brand) {
        //判断品牌名称和品牌log是否为空
        if (StringUtils.isEmpty(brand.getName())) {
            return false;
        }
        if (brand.getImage().isEmpty()) {
            return false;
        }

        brandMapper.updateByPrimaryKeySelective(brand);


        return true;
    }

    /**
     * 品牌删除
     *
     * @param brandIds
     * @return
     */
    @Override
    public void delete(String[] brandIds) {


        //判断ids是否为空
        if (brandIds == null && brandIds.length == 0) {
            new RuntimeException("id为空或不存在");
        }

        //遍历brandIds,循环删除

        for (String brandId : brandIds) {
            brandMapper.deleteByPrimaryKey(brandId);
        }
    }
}
